initMainMap();

function initMainMap() {
    map = new BMap.Map("bd-map1");
    // 在创建地图实例后，我们需要对其进行初始化，BMap.Map.centerAndZoom()方法要求设置中心点坐标和地图级别。 地图必须经过初始化才可以执行其他操作。
    map.centerAndZoom(new BMap.Point(110.00, 18.53), 12);
    map.setCurrentCity("陵水黎族自治县");
    map.enableScrollWheelZoom(); //启用滚轮放大缩小，默认禁用
    var mapStyle = {
        style: "midnight"
    };
    map.setMapStyle(mapStyle);
    initSectionPoint();

}

//初始化地图中的标段坐标
function initSectionPoint() {
    setTimeout(function () {
        var projectData = [{
                "sectionGenus": "subject_category_01",
                "zlgovUnitName": "陵水黎族自治县建设工程质量监督站",
                "provinceCode": "460000",
                "origin": "rklx_2",
                "buildingUnitName": "陵水县建设单位",
                "sectionArea": "460100",
                "sectionCode": "ZHGD46AL1000071000072",
                "sectionId": "402881896ae395b3016ae3c79f73001c",
                "sectionAddress": "陵水",
                "sectionName": "海南清水湾B11-1、B11-2区2栋公寓、4栋高层及地下车库工程",
                "aqgovUnitName": "陵水黎族自治县建设工程质量监督站",
                "areaCode": "460101",
                "supervisionUnitName": "陵水县监理单位",
                "sectionXxjd": "xxjd_202",
                "sectionGenusName": "房地产项目",
                "constructionUnitName": "陵水县施工企业",
                "gpsX": "110.099081",
                "gpsY": "18.508767 ",
                "status": "0",
                "subjectName": "海航YOHO湾"
            },
            {
                "sectionGenus": "subject_category_01",
                "zlgovUnitName": "陵水黎族自治县建设工程质量监督站",
                "provinceCode": "460000",
                "origin": "rklx_2",
                "buildingUnitName": "陵水县建设单位",
                "sectionArea": "460100",
                "sectionCode": "SECTION201905140004",
                "sectionId": "402881896ae395b3016ae3bfd5e00013",
                "sectionAddress": "陵水",
                "sectionName": "海航YOHO湾(A1,A2,A3,A5,A6,地下室)",
                "aqgovUnitName": "陵水黎族自治县建设工程质量监督站",
                "areaCode": "460101",
                "supervisionUnitName": "陵水县监理单位",
                "sectionXxjd": "xxjd_202",
                "sectionGenusName": "房地产项目",
                "constructionUnitName": "陵水县施工企业",
                "gpsX": "110.119203",
                "gpsY": "18.552643",
                "status": "0",
                "subjectName": "海航YOHO湾"
            },
            {
                "sectionGenus": "subject_category_01",
                "zlgovUnitName": "陵水黎族自治县建设工程质量监督站",
                "provinceCode": "460000",
                "origin": "rklx_2",
                "buildingUnitName": "陵水县建设单位",
                "sectionArea": "460100",
                "sectionCode": "SECTION0005",
                "sectionId": "671ccb70dbba40d2a5ef37ca9b35d50b",
                "sectionAddress": "海南",
                "sectionName": "雅居乐金沙湾健康教育新城项目A1401地块",
                "aqgovUnitName": "陵水黎族自治县建设工程质量监督站",
                "areaCode": "460101",
                "supervisionUnitName": "陵水县监理单位",
                "sectionXxjd": null,
                "sectionGenusName": "房地产项目",
                "constructionUnitName": "陵水县施工企业",
                "gpsX": "110.052513",
                "gpsY": "18.536739",
                "status": "0",
                "subjectName": "海航YOHO湾"
            }
        ];
        if (markers.length > 0) {
            markerClusterer.clearMarkers(markers);
        }
        markers = [];
        if (projectData == null) {
            return;
        }
        var sectionList = projectData;

        for (var i = 0; i < sectionList.length; i++) {
            if (sectionList[i].gpsX != null && sectionList[i].gpsY != null) {
                var point = new BMap.Point(sectionList[i].gpsX, sectionList[i].gpsY);
                addMarker(sectionList[i], point, markers);
            }
        }
        //设置点聚合
        // markerClusterer = new BMapLib.MarkerClusterer(map, {
        //     markers: markers
        // });
    }, 100)
}


function addMarker(sectionArea, point, markers) {
    //设置房建类的和市政类的颜色不同
    var myIcon = new BMap.Icon("/img/smartsite/images/location.png", new BMap.Size(34, 34), {
        anchor: new BMap.Size(17, 32)
    });
    var marker = new BMap.Marker(point, {
        icon: myIcon
    });
    //设置文本标注
    var opts = {
        position: point, // 指定文本标注所在的地理位置
        offset: new BMap.Size(0, 0) //设置文本偏移量
    }

    var content = "&nbsp;&nbsp;&nbsp<img src='/img/smartsite/images/u2404.png'>&nbsp;&nbsp" + sectionArea.sectionName + "&nbsp;&nbsp;&nbsp</br>" +
        "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp<a href='javascript:void(0);' style='color:#96C9FE' onclick='toSection(\"" + sectionArea.sectionId + "\")'>进入项目部</a>";
    // var content = "<div style ='float:left; margin-right: 10px;width:25%;height:86px;'><img src='/images/u2404.png' 'width ='100%' height ='100%'></div>" + "<div style='height:100%;'><h4> 工程信息 </h4><p><span style='font-size:10px'>" + sectionArea.sectionName + "</span> </p>" + "<p><a href = 'javascript:void(0);' style = 'color:#96C9FE' onclick = 'toSection(\"" + sectionArea.sectionId + "\")' > 进入项目部 </a></p>" + "</div>";



    var label = new BMap.Label(content, opts); // 创建文本标注对象
    label.setStyle({
        color: "white",
        fontSize: "14px",
        height: "28px",
        lineHeight: "28px",
        fontFamily: "微软雅黑",
        backgroundColor: "rgba(28, 43, 145, 1)",
        border: "1px solid rgba(0, 162, 255, 1)"
    });
    map.addOverlay(marker);
    var steelContent = "<h4 style='margin:0 0 5px 0;padding:0.2em 0'>工程名称：" + sectionArea.sectionName + "</h4>" +
        "<p style='margin:0;line-height:1.5;font-size:13px;'>地址：" + sectionArea.sectionAddress + "</p>"
    var steelOpts = {
        width: 0, //信息窗口宽度
        height: 0, //信息窗口高度
        title: "工程信息", //信息窗口标题
        enableMessage: true //设置允许信息窗发送短息
    }

    //添加鼠标滑过时打开自定义信息窗口事件
    marker.addEventListener("mouseover", function () {
        this.openInfoWindow(new BMap.InfoWindow(content));
    });
    markers.push(marker);
    markerArr.push(marker);
}

//进入项目部
function toSection(sectionId) {
    var oUrl;
    $.ajax({
        url: '../sso.rest/getSSOKey',
        type: "post",
        async: false,
        data: {
            "sectionId": sectionId
        },
        success: function (data) {
            oUrl = data.singleLoginUrl + "sso.html?ssoFlag=1&key=" + data.key + "&sectionId=" + sectionId;
            if ("govt_unit_type_02" == data.actiontype) {
                oUrl = data.singleLoginUrl + "sso.html?token=" + data.key;
            }
            if (data.ssoFlag) {
                oUrl = data.singleLoginUrl + "sso.html?token=" + data.token;
            }
        },
        error: function () {
            alert('获取标段详细信息失败');
        }
    });
    if (oUrl != undefined && oUrl != null) {
        var newTab = window.open('about:blank');
        newTab.location.href = oUrl;
    }
}

var map;
var markerArr = new Array();
var markers = [];
var markerClusterer = new BMapLib.MarkerClusterer(map, {
    markers: markers
});;